
//引入epress
const express = require('express');
//router
const router = express.Router();
const path = require('path');
const fs = require('fs');
// const url=require('url')

//渲染login.html
router.use(express.static(path.join(__dirname, 'view')))
router.use('/login', (req, res) => {
    res.sendFile(path.join(__dirname, ('/view/login.html')))
})

//读取数据
let dir = path.join(__dirname, 'persons.json');
let data = fs.readFileSync(dir, 'utf-8');
data = JSON.parse(data);
console.log(data);


router.use(express.urlencoded({ extended: false }));

//该中间件接受的是 前端传参的格式 Content-Type: application/json
// {"username":"小红","password":"1234567"}
router.use(express.json())

router.post('/dologin', (req, res) => {
    console.log(req.body);
    let user = req.body
    let s = data.find(item => item.username == user.username && item.password == user.password
    )
    if (s) {
        res.send('登陆成功')
    } else {
        res.send('登录失败')
    }
})


//暴露
module.exports = router;